package com.djinfo.work.util;

import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.RenderedImage;
import java.io.*;
import java.net.URL;

/**
 * Created by Administrator on 2016/12/23.
 */
public class FileHtml {
    private static final Log log = LogFactory.getLog(FileHtml.class);

    public static void main(String[] args) throws Exception {

        int count=1213211312;
        int  avg=count/21131241;
        System.out.println(avg);


        String name="ddaaa";
        System.out.print(name.substring(1,5));
    }

    /**
     * 创建文件
     *
     * @throws IOException
     */
    public  String creatTxtFile(String name, String path) throws IOException {
        String filenameTemp = path + "/" + MD5.stringToMD5(name) + ".html";
        getfile(path);
        File filename = new File(filenameTemp);
        if (!filename.exists()) {
            filename.createNewFile();
        } else {
            log.debug("文件：" + name + "  --          路径：" + filenameTemp + "  已存在。");
        }
        return filenameTemp;
    }

    /**
     * 创建文件夹
     *
     * @param filepath
     */
    public  void getfile(String filepath) {
        File yearfile = new File(filepath);
        // 如果文件夹不存在则创建
        if (!yearfile.exists() && !yearfile.isDirectory()) {
            yearfile.mkdirs();
        }
    }


    /**
     * 判断是否存在
     * @return
     */
   public  void isExists(String path) throws Exception {
       String pathFile=path+"/"+"images/directory-nav.png";
       File file=new File(pathFile);
       if(!file.exists()){
           getfile(path+"/"+"images");
           GenerateImage(pathFile);
       }
       String pathJs=path+"/"+"js.js";
       File fileJs=new File(pathJs);
       if(!fileJs.exists()){
           GenerateJs(pathJs);
       }
       String pathCss=path+"/"+"css.css";
       File fileCss=new File(pathCss);
       if(!fileCss.exists()){
           GenerateCss(pathCss);
       }
       String pathJquery=path+"/"+"jquery.js";
       File fileJquery=new File(pathJquery);
       if(!fileJquery.exists()){
           GenerateJquery(pathJquery);
       }
   }

    /**
     *   对字节数组字符串进行Base64解码并生成图片
     * @param path   路径
     * @return
     */
    public  void GenerateImage(String path) throws IOException {

        try {
            Image[] array = new Image[10];
             URL url=  this.getClass().getResource("/directory-nav.png");
            Image image = ImageIO.read(new File(url.toURI()));
            array[0] = image;
            ImageIO.write((RenderedImage) image, "png", new File(path));
        } catch (Exception e) {
            System.out.println("无法创建新文件！");
            e.printStackTrace();
        }finally {

        }
    }


    /**
     * 生成js
     * @param path
     */
    public   void GenerateJs(String path) throws IOException {
      String  jsCount=" function DirectoryNav($h,config){\n" +
              "        this.opts = $.extend(true,{\n" +
              "            scrollThreshold:0.5,    //滚动检测阀值 0.5在浏览器窗口中间部位\n" +
              "            scrollSpeed:1,        //滚动到指定位置的动画时间\n" +
              "            scrollTopBorder:500,    //滚动条距离顶部多少的时候显示导航，如果为0，则一直显示\n" +
              "            easing: 'swing',        //不解释\n" +
              "            delayDetection:2,     //延时检测，避免滚动的时候检测过于频繁\n" +
              "            scrollChange:function(){}\n" +
              "        },config);\n" +
              "        this.$win = $(window);\n" +
              "        this.$h = $h;\n" +
              "        this.$pageNavList = \"\";\n" +
              "        this.$pageNavListLis =\"\";\n" +
              "        this.$curTag = \"\";\n" +
              "        this.$pageNavListLiH = \"\";\n" +
              "        this.offArr = [];\n" +
              "        this.curIndex = 0;\n" +
              "        this.scrollIng = false;\n" +
              "        this.init();\n" +
              "    }\n" +
              "\n" +
              "    DirectoryNav.prototype = {\n" +
              "        init:function(){\n" +
              "            this.make();\n" +
              "            this.setArr();\n" +
              "            this.bindEvent();\n" +
              "        },\n" +
              "        make:function(){\n" +
              "            //生成导航目录结构,这是根据需求自己生成的。如果你直接在页面中输出一个结构那也挺好不用 搞js\n" +
              "            $(\"body\").append('<div class=\"directory-nav\" id=\"directoryNav\"><ul></ul><span class=\"cur-tag\"></span><span class=\"c-top\"></span><span class=\"c-bottom\"></span><span class=\"line\"></span></div>>');\n" +
              "            var $hs = this.$h,\n" +
              "                $directoryNav = $(\"#directoryNav\"),\n" +
              "                temp = [],\n" +
              "                index1 = 0,\n" +
              "                index2 = 0;\n" +
              "            $hs.each(function(index){\n" +
              "                var $this = $(this),\n" +
              "                        text = $this.text();\n" +
              "                if(this.tagName.toLowerCase()=='h2'){\n" +
              "                    index1++;\n" +
              "                    if(index1%2==0) index2 = 0;\n" +
              "                    temp.push('<li class=\"l1\"><span class=\"c-dot\"></span>'+index1+'. <a class=\"l1-text\">'+text+'</a></li>');\n" +
              "                }else{\n" +
              "                    index2++;\n" +
              "                    temp.push('<li class=\"l2\">'+index1+'.'+index2+' <a class=\"l2-text\">'+text+'</a></li>');\n" +
              "\n" +
              "                }\n" +
              "            });\n" +
              "            $directoryNav.find(\"ul\").html(temp.join(\"\"));\n" +
              "\n" +
              "            //设置变量\n" +
              "            this.$pageNavList = $directoryNav;\n" +
              "            this.$pageNavListLis = this.$pageNavList.find(\"li\");\n" +
              "            this.$curTag = this.$pageNavList.find(\".cur-tag\");\n" +
              "            this.$pageNavListLiH = this.$pageNavListLis.eq(0).height();\n" +
              "\n" +
              "            if(!this.opts.scrollTopBorder){\n" +
              "                this.$pageNavList.show();\n" +
              "            }\n" +
              "        },\n" +
              "        setArr:function(){\n" +
              "            var This = this;\n" +
              "            this.$h.each(function(){\n" +
              "                var $this = $(this),\n" +
              "                    offT = Math.round($this.offset().top);\n" +
              "                This.offArr.push(offT);\n" +
              "            });\n" +
              "        },\n" +
              "        posTag:function(top){\n" +
              "            this.$curTag.css({top:top+'px'});\n" +
              "        },\n" +
              "        ifPos:function(st){\n" +
              "            var offArr = this.offArr;\n" +
              "            //console.log(st);\n" +
              "            var windowHeight = Math.round(this.$win.height() * this.opts.scrollThreshold);\n" +
              "            for(var i=0;i<offArr.length;i++){\n" +
              "                if((offArr[i] - windowHeight) < st) {\n" +
              "                    var $curLi = this.$pageNavListLis.eq(i),\n" +
              "                        tagTop = $curLi.position().top;\n" +
              "                    $curLi.addClass(\"cur\").siblings(\"li\").removeClass(\"cur\");\n" +
              "                    this.curIndex = i;\n" +
              "                    this.posTag(tagTop+this.$pageNavListLiH*0.5);\n" +
              "                    //this.curIndex = this.$pageNavListLis.filter(\".cur\").index();\n" +
              "                    this.opts.scrollChange.call(this);\n" +
              "                }\n" +
              "            }\n" +
              "        },\n" +
              "        bindEvent:function(){\n" +
              "            var This = this,\n" +
              "                show = false,\n" +
              "                timer = 0;\n" +
              "            this.$win.on(\"scroll\",function(){\n" +
              "                var $this = $(this);\n" +
              "                clearTimeout(timer);\n" +
              "                timer = setTimeout(function(){\n" +
              "                    This.scrollIng = true;\n" +
              "                    if($this.scrollTop()>This.opts.scrollTopBorder){\n" +
              "                        if(!This.$pageNavListLiH) This.$pageNavListLiH = This.$pageNavListLis.eq(0).height();\n" +
              "                        if(!show){\n" +
              "                            This.$pageNavList.fadeIn();\n" +
              "                            show = true;\n" +
              "                        }\n" +
              "                        This.ifPos( $(this).scrollTop() );\n" +
              "                    }else{\n" +
              "                        if(show){\n" +
              "                            This.$pageNavList.fadeOut();\n" +
              "                            show = false;\n" +
              "                        }\n" +
              "                    }\n" +
              "                },This.opts.delayDetection);\n" +
              "            });\n" +
              "\n" +
              "            this.$pageNavList.on(\"click\",\"li\",function(){\n" +
              "                var $this = $(this),\n" +
              "                    index = $this.index();\n" +
              "                This.scrollTo(This.offArr[index]);\n" +
              "            })\n" +
              "        },\n" +
              "        scrollTo: function(offset,callback) {\n" +
              "            var This = this;\n" +
              "            $('html,body').animate({\n" +
              "                scrollTop: offset\n" +
              "            }, this.opts.scrollSpeed, this.opts.easing, function(){\n" +
              "                This.scrollIng = false;\n" +
              "                //修正弹两次回调 蛋疼\n" +
              "                callback && this.tagName.toLowerCase()=='body' && callback();\n" +
              "            });\n" +
              "        }\n" +
              "    };\n" +
              "\n" +
              "    //实例化\n" +
              "    var directoryNav = new DirectoryNav($(\"h2,h3\"),{\n" +
              "        scrollTopBorder:0   //滚动条距离顶部多少的时候显示导航，如果为0，则一直显示\n" +
              "    });\n";
        OutputStreamWriter myNewFile= null;
        try {
            myNewFile=new OutputStreamWriter(new FileOutputStream(path),"UTF-8");
            myNewFile.write(jsCount);
        } catch (Exception e) {
            System.out.println("无法创建新文件！");
            e.printStackTrace();
        }finally {
            myNewFile.close();
        }
}


    /**
     * 生成css
     * @param path
     */
    public   void GenerateCss(String path) throws IOException {
        String  jsCount="   html,body,h3{ margin: 0; padding: 0;}\n" +
                "        h2,h3{ }\n" +
                "        li{ list-style: none; margin: 0; vertical-align: top;}\n" +
                "        ul{ margin: 0; padding: 0;}\n" +
                "\n" +
                "        /*这是百科右侧导航代码的样式表*/\n" +
                "        .directory-nav{position: fixed; right:50px; bottom:40px; padding: 12px 0 20px 32px; display: none;}\n" +
                "        .directory-nav li{height: 34px;line-height: 34px;  position: relative;cursor:pointer;}\n" +
                "        .directory-nav li a{}\n" +
                "        .directory-nav .l1{ }\n" +
                "        .directory-nav .l2{ text-indent:0.6em;}\n" +
                "        .directory-nav .l1 a{font-size: 16px;}\n" +
                "        .directory-nav .l2 a{font-size: 14px;}\n" +
                "        .directory-nav,.directory-nav a{ color: #666;}\n" +
                "        .directory-nav .cur a{ color: #4f9cee;}\n" +
                "\n" +
                "        .directory-nav .line{ position: absolute; left:4px; top: 5px; bottom: 5px; z-index: 1; width: 2px; background: #ddd;}\n" +
                "        .directory-nav .c-top,.directory-nav .c-bottom{ position: absolute; left: 0; z-index: 2;\n" +
                "            display: block; width: 10px; height: 10px; font-size: 0; line-height: 0;\n" +
                "            background: url(images/directory-nav.png) no-repeat 0 -69px;\n" +
                "        }\n" +
                "        .directory-nav .c-dot{\n" +
                "            position: absolute; left:-32px; top: 50%; z-index: 2; margin-top: -5px;\n" +
                "            display: block; width: 10px; height: 10px; font-size: 0; line-height: 0;\n" +
                "            background: url(images/directory-nav.png) no-repeat -275px 0;\n" +
                "        }\n" +
                "\n" +
                "        .directory-nav .c-top{ top: 0}\n" +
                "        .directory-nav .c-bottom{ bottom: 0}\n" +
                "        .directory-nav .cur-tag{\n" +
                "            position: absolute; left: -1px; top:30px; z-index: 5; margin-top: -6px;\n" +
                "            display: block; width: 19px; height: 13px; font-size: 0; line-height: 0;\n" +
                "            background: url(images/directory-nav.png) no-repeat -271px -37px;\n" +
                "            -webkit-transition:top .3s ease 0s;\n" +
                "            transition:top .3s ease 0s;\n" +
                "        }\n";
        OutputStreamWriter myNewFile= null;
        try {
            myNewFile = new OutputStreamWriter(new FileOutputStream(path),"UTF-8");
            myNewFile.write(jsCount);
        } catch (Exception e) {
            System.out.println("无法创建新文件！");
            e.printStackTrace();
        }finally {
            myNewFile.close();
        }
    }


    /**
     * 生成Jquery
     * @param path
     */
    public  void GenerateJquery(String path) throws IOException {
        InputStream is= null;
        OutputStreamWriter myNewFile= null;
        try {
            is = getClass().getResourceAsStream("/Jquery.js");
           String   jsCount = IOUtils.toString(is, "UTF-8");
            is.close();
            myNewFile = new OutputStreamWriter(new FileOutputStream(path),"UTF-8");
            myNewFile.write(jsCount);
        } catch (Exception e) {
            System.out.println("无法创建新文件！");
            e.printStackTrace();
        }finally {
            myNewFile.close();
        }
    }
}

